package org.robot.power;

import java.io.File;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.robot.Spider;
import org.robot.component.Attachment;
import org.robot.component.Page;
import org.robot.component.Rule;
import org.robot.utils.HttpDownload;
import org.robot.utils.MD5;
import org.robot.utils.MatchEmail;
import org.robot.utils.PageUtil;

public class Downloader implements Runnable {
    private static Log log = LogFactory.getLog(Downloader.class);

    private Spider spider;
    private Attachment attachment;
    private File destFile;

    public Downloader(Spider spider, Attachment attachment, File destFile) {
        this.spider = spider;
        this.attachment = attachment;
        this.destFile = destFile;
    }

    @Override
    public void run() {
        try {
        	Document doc = PageUtil.downDoc(attachment.getUrl(), destFile);
        	if (doc != null) {
        		Page page = new Page(doc, attachment);
        		Rule rule = spider.getRegulation().getRule(attachment.isSeed(), attachment.getType());
        		if (rule != null && rule.getType().equals("_pdf_"))
        			page.eleStream(rule).findAny().ifPresent(e -> parsePdf(page, e.absUrl("href")));
        		spider.parse(page);
        		if (rule == null || rule.getType().equals("_pdf_"))
        			return;
        		List<Attachment> list = rule.isAddByHand() ? page.getNextAttachmentList() : page.applyRule(rule);
        		if (!StringUtil.isBlank(rule.getType()))
        			list.forEach(attachment -> attachment.setType(rule.getType()));
        		log.info(attachment.getType() + " " + attachment.getUrl() + " 取到下层链接个数：" + list.size());
        		spider.getSchedule().addAll(list);
        	}
        } catch (Exception e) {
            log.error(attachment.getUrl(), e);
            spider.getSchedule().remove(attachment);
        }
    }

	private void parsePdf(Page page, String pdfUrl) {
		log.info("下载或读取pdf：" + pdfUrl);
		String pdfname = MD5.getMD5(pdfUrl) + ".pdf";
		HttpDownload.saveContent(pdfUrl, "utf-8", destFile.getParent(), pdfname);
		String pdfContent = MatchEmail.getContent(new File(destFile.getParent(), pdfname));
		Set<String> emailSet = MatchEmail.getEmailByStr(pdfContent);
		page.put("pdfContent", pdfContent);
		page.put("emailSet", emailSet);
		page.put("pdfUrl", pdfUrl);
	}


}
